
import Const from '@/core/const';
const Modules = import.meta.glob(`../assets/images/*/*`, { eager: true });
export default {
   /**
    * @description 获取图片文件
    * @param 文件名称 file
    * @param 精确文件名称 precise_file
   */
    getImageFile(file, precise_file, type = 'png') {  
        const path = `../assets/images/${ file }/${ precise_file }.${ type }`;
        return Modules[path].default
    },
    /* =============== 图片内容处理 ================ */     
    imageFilter(item) {
        if (!item || typeof item !== 'string') {
            return ''
        }
        if (item.includes(',')) {
            let arr = item.split(',')
            item = arr[0]
        }
        if (item.includes("http")) {
            return item
        } else {
            return Const.NET.FILE_URL_PREFIX + item
        }
    },
    isExternal(path) {
      return /^(https?:|mailto:|tel:)/.test(path)
    },
    ifImageUrl(url, host = '') {
        if (url) {
            return host + url
        } else {
            return Modules[`../assets/images/home/default-img.png`].default
        }
    },
    /**
    * 创建 <a> 标签下载文件流格式图片
    * @param file 文件流
    * @param fileName 文件名称
    */
    downloadFile(file, fileName = 'file') {
        const blob = new Blob([file]);
        const fileReader = new FileReader();
        fileReader.readAsDataURL(blob);
        fileReader.onload = (e) => {
            const a = document.createElement("a");
            a.download = fileName;
            a.href = e.target?.result;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        };
    },
    /**
    * 根据图片路径下载
    * @param downloadUrl 图片路径
    * @param name 文件名称
    */
    downloadImage(downloadUrl, name = 'pic') {
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.setAttribute('download', name);
        document.body.appendChild(link);
        link.click();
        link.remove();
    },
    /* =============== 图片内容处理 ================ */
    /**
    * 裁剪图片
    * @param imgWidth 图片宽度
    * @param imgHeight 图片高度
    * @param canvasWidth canvas宽度
    * @param canvasHeight canvas高度
    * @param type 类型
    */
    imgCropping(imgWidth, imgHeight, canvasWidth, canvasHeight, type = 'cover') {
        let obj = {}
        let sx, sy, sw, sh, imgRatio, canvasRatio;// sx:图片x轴位置, sy:图片y轴位置, sw:图片宽度, sh:图片高度
        imgRatio = imgWidth / imgHeight
        canvasRatio = canvasWidth / canvasHeight;
        if (type === 'contain') {
            if (imgRatio <= canvasRatio) {
                sw = imgRatio * canvasWidth
                sh = canvasHeight
                sx = (canvasWidth - sw) / 2
                sy = 0
            } else {
                sw = canvasWidth
                sh = sw / imgRatio
                sx = 0
                sy = (canvasHeight - sh) / 2 
            }
        } else if (type === 'cover') {
            if (imgRatio <= canvasRatio) {
                sw = imgWidth
                sh = sw / canvasRatio
                sx = 0
                sy = (imgHeight - sh) / 2
            } else {
                sh = imgHeight
                sw = sh * canvasRatio
                sx = (imgWidth - sw) / 2
                sy = 0
            }
        }
        obj = { sx, sy, sw, sh }
        return obj
    },
    // 图片cdn
    imgCdn(path){
      const url = import.meta.env.VITE_CDN_POINT
      if(path){
        return url + path
      }
    }
}

